package com.unicom.projt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
import com.unicom.projt.db.Account;
import com.unicom.projt.db.DB;
import com.unicom.projt.db.DBConfig;
import com.unicom.projt.db.RunRecord;
import com.unicom.projt.fee.PaymentConfirmPage;
import com.unicom.projt.fee.PaymentPage;
import com.unicom.projt.fee.PaymentSuccessPage;
import com.unicom.projt.http.HttpConnection;
import com.unicom.projt.ivr.BindCardPage;
import com.unicom.projt.reg.RegisterStepOnePage;
import com.unicom.projt.util.UserHolder;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-1-10
 * Time: 下午4:01
 */
public class SplashActivity extends Activity {
    int runLogSize=0;
    Account rememberAccount=null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        DB db = DB.getInstance(this).open();
        runLogSize = db.queryRunLogRecordSize();
        if (runLogSize == 0) {
            RunRecord runRecord=new RunRecord();
            runRecord.runDate=new Date().toString();
            db.insertRunRecord(runRecord);
        }
        rememberAccount =db.queryRememberAccount();
        db.close();
        HttpConnection.assetManager = getAssets();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                Intent mainIntent;
                if(runLogSize ==0){
//                if(true){
//                    mainIntent = new Intent(SplashActivity.this,RegisterStepOnePage.class);
                    mainIntent = new Intent(SplashActivity.this,CaptionPage.class);
                }else{
                    if(rememberAccount==null){
//                        UserHolder.phoneNumber="18602800512";

//                         mainIntent = new Intent(SplashActivity.this, PaymentSuccessPage.class);
//                        mainIntent.putExtra("phoneNumber",UserHolder.phoneNumber);
//                        mainIntent.putExtra("amount",100D);
//                        mainIntent.putExtra("orderId",UserHolder.phoneNumber);
//                        mainIntent.putExtra("payPkg",UserHolder.phoneNumber);
//                        mainIntent.putExtra("password",UserHolder.phoneNumber);
//                        UserHolder.isBindUser=true;

                         mainIntent = new Intent(SplashActivity.this, InputPhoneNumberPage.class);
                    }else {
                        UserHolder.phoneNumber = rememberAccount.phoneNumber;
                        mainIntent = new Intent(SplashActivity.this, Home.class);
                    }
                }
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, 2000);
    }

    @Override
    protected void onResume() {
        showLoading();
        super.onResume();
        onBackPressed();
    }

    private void showLoading() {
        int duration = Toast.LENGTH_SHORT;
        CharSequence sc = "正在启动沃快充....";
        Toast toast = Toast.makeText(this, sc, duration);
        toast.getView().setBackgroundDrawable(getResources().getDrawable(R.drawable.splash));
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.show();
    }
}
